<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

class IndexController extends BaseController {

    /**
     * 后台首页
     */
    public function index() {
//        $this->pushVersion();
        $actRs = session('act_list');
        $menuRs = getMenuList($actRs);
        $this->assign('menu_list', $menuRs);
        $adminInfo = getAdminInfo(session('admin_id'));
        //$orderAmount = M('order')->where("order_status=0 and (pay_status=1 or pay_code='cod')")->count();
        $this->assign('order_amount', $orderAmount);
        $this->assign('adminInfo', $adminInfo);
        $this->display();
    }

    /**
     * 欢迎页
     */
    public function welcome() {
        $today = strtotime("-1 day");
        $userMod = M('users');
        $questionMod = M('question');
        $answerMod = M('answer');
        $accMod = M('accountant_users');
        //$count['handle_order'] = $orderMod->where("order_status=0 and (pay_status=1 or pay_code='cod')")->count(); //待处理订单
        //$count['new_order'] = $orderMod->where("add_time>$today")->count(); //今天新增订单
       // $count['goods'] = M('goods')->where("1=1")->count(); //商品总数
        //$count['article'] = M('article')->where("1=1")->count(); //文章总数
        $count['users'] = $userMod->where("1=1")->count(); //会员总数
        $count['total_question'] = $questionMod->where("1=1")->count(); //会员总数
        $count['total_answer'] = $answerMod->where("1=1")->count(); //会员总数
        $count['total_acc'] = $accMod->where("1=1")->count(); //会员总数
       // $count['today_login'] = $userMod->where("last_login>$today")->count(); //今日访问
        $count['today_question'] = $userMod->where("reg_time>$today")->count(); //今日访问
        $count['today_answer'] = $userMod->where("reg_time>$today")->count(); //今日访问
        $count['today_accountant'] = $accMod->where("reg_time>$today")->count(); //今日访问
        $count['new_users'] = $userMod->where("reg_time>$today")->count(); //新增会员
        //$count['comment'] = M('comment')->where("is_show=0")->count(); //最新评论
        $this->assign('sysInfo', $this->getSysInfo());
        $this->assign('count', $count);
        $this->display();
    }

    /**
     * 获取系统信息
     * @return array
     */
    public function getSysInfo() {
        $sysInfo['os'] = PHP_OS;
        $sysInfo['zlib'] = function_exists('gzclose') ? 'YES' : 'NO'; //zlib
        $sysInfo['safe_mode'] = (boolean) ini_get('safe_mode') ? 'YES' : 'NO'; //safe_mode = Off
        $sysInfo['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sysInfo['curl'] = function_exists('curl_init') ? 'YES' : 'NO';
        $sysInfo['web_server'] = $_SERVER['SERVER_SOFTWARE'];
        $sysInfo['phpv'] = phpversion();
        $sysInfo['ip'] = GetHostByName($_SERVER['SERVER_NAME']);
        $sysInfo['fileupload'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown';
        $sysInfo['max_ex_time'] = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sysInfo['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sysInfo['domain'] = $_SERVER['HTTP_HOST'];
        $sysInfo['memory_limit'] = ini_get('memory_limit');
        $sysInfo['version'] = file_get_contents('./Application/Admin/Conf/version.txt');
        $mysqlinfo = M()->query("SELECT VERSION() as version");
        $sysInfo['mysql_version'] = $mysqlinfo['version'];
        if (function_exists("gd_info")) {
            $gd = gd_info();
            $sysInfo['gdinfo'] = $gd['GD Version'];
        } else {
            $sysInfo['gdinfo'] = "未知";
        }
        return $sysInfo;
    }

    /**
     * 版本
     */
    public function pushVersion() {
        if (!empty($_SESSION['isset_push']))
            return false;
        $_SESSION['isset_push'] = 1;
        error_reporting(0); //关闭所有错误报告
        $appPath = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
        $versionPath = $appPath . '/Application/Admin/Conf/version.txt';
        $curentVersion = file_get_contents($versionPath);

        $vaules = array(
            'domain' => $_SERVER['SERVER_NAME'],
            'last_domain' => $_SERVER['SERVER_NAME'],
            'key_num' => $curentVersion,
            'install_time' => INSTALL_DATE,
            'serial_number' => SERIALNUMBER,
        );
        $url = "http://service.xiarikui.com/index.php?m=Home&c=Index&a=user_push&" . http_build_query($vaules);
        stream_context_set_default(array('http' => array('timeout' => 3)));
        file_get_contents($url);
    }

    /**
     * 获取角色菜单
     * @param $actRs
     * @return array
     */
    public function getRoleMenu($actRs) {
        $modules = $roleMenu = array();
        $rs = M('sys_mod')->where('level>1 AND visible=1')->order('orderby ASC')->select();

        if ($actRs == 'all') {
            foreach ($rs as $row) {
                if ($row['level'] == 3) {
                    $row['url'] = U("Admin/" . $row['ctl'] . "/" . $row['act'] . "");
                    $modules[$row['parent_id']][] = $row; //子菜单分组
                }
                if ($row['level'] == 2) {
                    $pmenu[$row['mod_id']] = $row; //二级父菜单
                }
            }
        } else {
            $actRs = explode(',', $actRs);
            foreach ($rs as $row) {
                if (in_array($row['mod_id'], $actRs)) {
                    $row['url'] = U("Admin/" . trim($row['ctl']) . "/" . $row['act'] . "");
                    $modules[$row['parent_id']][] = $row; //子菜单分组
                }
                if ($row['level'] == 2) {
                    $pmenu[$row['mod_id']] = $row; //二级父菜单
                }
            }
        }
        $keys = array_keys($modules); //导航菜单
        foreach ($pmenu as $k => $val) {
            if (in_array($k, $keys)) {
                $val['submenu'] = $modules[$k]; //子菜单
                $roleMenu[] = $val;
            }
        }

        return $roleMenu;
    }

    /**
     * 修改指定表数据字段
     */
    public function changeTableVal() {
        $table = I('table'); // 表名
        $idName = I('id_name'); // 表主键id名
        $idValue = I('id_value'); // 表主键id值
        $field = I('field'); // 修改哪个字段
        $value = I('value'); // 修改字段值                        
        M($table)->where("$idName = $idValue")->save(array($field => $value));
    }

}
